"use strict";

/**
 * Created by xuchao on 15/10/16.
 */
angular.module('cloudatum.util', ['ngResource','toaster'])
  .factory('Toast', function (toaster) {
    return {
      success: function (text) {
        toaster.success('', text, 2000);
      },
      fail: function (text) {
        toaster.warning('', text, 2000);
      },
      error: function (text) {
        toaster.error('', text?text:'网络错误', 2000);
      },
      message: function (text) {
        toaster.info('', text, 2000);
      }
    };
  })
  .factory('RestService', function ($resource) {
    var request = function (url) {
      return $resource(null, null, {
        check: {
          url: url,
          method: 'POST',
          isArray: true
        },
        submitWithToken: {
          url: url,
          method: 'POST',
          isArray: false
        },
        stringSubmitWithToken: {
          url: url,
          method: 'POST',
          isArray: false,
          transformResponse: function (data) {
            return {result: data};
          }
        }
      });
    };
    return {
      getInfo: function (activityId) {
        return $resource('../activity/voteitem/getActivityInfo').get({activityId:activityId});
      },
      queryItems: function (activityId, searchType, page, size) {
        return request('../activity/voteitem/itemList').check(null, {activityId:activityId,searchType:searchType,pageNum:page,pageSize:size});
      },
      vote: function (activityId, openid, id) {
        return $resource('../activity/voteitem/vote').save(null, {activityId:activityId, itemId:id, openid:openid});
      },
      getItem: function (id) {
        return $resource('../activity/voteitem/getItemInfo').get({id:id});
      },
      getWxConfig: function (url, originalId) {
        return $resource('../weixin/js/config').get({url:url, original_id:originalId});
      },
      rank: function (activityId, pageNum, pageSize) {
        return request('../activity/voteitem/itemList').check(null, {activityId:activityId, pageNum:pageNum, pageSize:pageSize});
      }
    };

  });
